package com.working.wanandroid.main

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.LinearLayout
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.core.graphics.toColorInt
import androidx.lifecycle.observe
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.DividerItemDecoration
import com.noober.background.drawable.DrawableCreator
import com.working.libbase.base.BaseActivity
import com.working.libbase.model.BackupChangeEvent
import com.working.libbase.storage.Storage
import com.working.libcommon.navigation.ReusingFragmentNavigator
import com.working.libcommon.bus.LiveDataBus
import com.working.wanandroid.R
import com.working.wanandroid.databinding.ActivityMainBinding
import com.working.wanandroid.main.backup.BackupAdapter
import com.working.libcommon.util.NetworkStatusMonitor
import com.working.libcommon.util.loge
import dp
import kotlin.properties.Delegates

class MainActivity : BaseActivity() {

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, MainActivity::class.java)
            context.startActivity(starter)
        }
    }

    private val mViewModel by viewModels<MainViewModel>()

    private val mBinding by binding<ActivityMainBinding>(R.layout.activity_main)

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            return toggle.onOptionsItemSelected(item)
        }
        return super.onOptionsItemSelected(item)
    }

    private val backupAdapter by lazy { BackupAdapter() }

    private var toggle: ActionBarDrawerToggle by Delegates.notNull()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Storage.model.loge()
        NetworkStatusMonitor.netEvent.observe(
            this
        ) {
            it.loge()
        }
        mBinding.apply {
            supportActionBar?.setDisplayHomeAsUpEnabled(
                mViewModel.mBackUpList?.isNotEmpty() == true
            )
            supportActionBar?.setHomeAsUpIndicator(
                R.drawable.ic_more_horiz
            )
            toggle = ActionBarDrawerToggle(
                this@MainActivity,
                drawer,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close
            ).apply {
                drawer.addDrawerListener(this)
                syncState()
            }
            backup.addItemDecoration(
                DividerItemDecoration(
                    this@MainActivity,
                    LinearLayout.VERTICAL
                ).apply {
                    setDrawable(
                        DrawableCreator.Builder()
                            .setSizeWidth(-1f)
                            .setSizeHeight(20f.dp)
                            .setSolidColor("#eeeeee".toColorInt())
                            .build()
                    )
                })
            backup.adapter = backupAdapter
            LiveDataBus.with<BackupChangeEvent>()
                .observe(this@MainActivity) {
                    when (it.operator) {
                        -1 -> {
                            it.backUpModel?.let { backUpModel ->
                                mViewModel.mBackUpList?.add(
                                    0,
                                    backUpModel
                                )
                            }
                            backupAdapter.notifyDataSetChanged()
                        }
                        else -> {
                            mViewModel.mBackUpList?.removeAt(
                                it.operator
                            )
                            backupAdapter.notifyDataSetChanged()
                        }
                    }
                    if (mViewModel.mBackUpList.isNullOrEmpty()) drawer.closeDrawers()
                    supportActionBar?.setDisplayHomeAsUpEnabled(
                        mViewModel.mBackUpList?.isNotEmpty() == true
                    )
                }
            backupAdapter.submitList(mViewModel.mBackUpList)

            lifecycleOwner = this@MainActivity
            val navHostFragment =
                supportFragmentManager.findFragmentById(
                    R.id.navHostFragment
                ) as NavHostFragment
            val navController =
                navHostFragment.navController
            val reusingFragmentNavigator =
                ReusingFragmentNavigator(
                    this@MainActivity,
                    supportFragmentManager,
                    navHostFragment.id
                )
            navController.navigatorProvider.addNavigator(
                reusingFragmentNavigator
            )
            mViewModel.buildGraph(navController)
            mViewModel.buildNav(bottomNav)

            bottomNav.setupWithNavController(
                navController
            )
        }
    }


}